<script setup>
import {ref,defineEmits,defineProps} from "vue";
import {useRouter} from "vue-router"
import { pcaTextArr } from "element-china-area-data";
import request  from '@/api/request'
let router = useRouter();

let emits=defineEmits(["ref-table"])
let props=defineProps(["data"])
let acceptanceOnArrival=ref({...props.data})
let warehouse=ref([])
let inboundDetails=ref([...acceptanceOnArrival.value.inboundDetailsList])
let User=ref([])



let onSubmit=()=>{
        request({
            url:"/acceptanceOnArrival",
            method:"put",
            data:acceptanceOnArrival.value
        }).then(res=>{
        })
        request({
            url:"/inboundDetails",
            method:"put",
            data:inboundDetails.value
        }).then(res=>{
            emits("ref-table")
        })
}


let location=()=>{
    request({
        //查询员工
            url:"/system/user/list",
            method:"post",
            data:{
                current:1,
                size:1000
            }
        }).then(res=>{
            User.value=res.data.records
        })
    request({
        //查询采购订单
        url:"/purchaseOrder",
        method:"get",
        params:{
            purchaseOrderId:acceptanceOnArrival.value.acceptanceOnArrivalPurchaseOrderId
        }
    }).then(res=>{
        for(let i = 0;i<res.data.records[0].purchaseDetailsList.length;i++){
            for(let o = 0;o<inboundDetails.value.length;o++){
                if(res.data.records[0].purchaseDetailsList[i].purchaseDetailsId==inboundDetails.value[o].inboundDetailsPurchaseDetailsId){
                    inboundDetails.value[o].supplierPrice=res.data.records[0].purchaseDetailsList[i].supplierPrice;
                    inboundDetails.value[o].purchaseDetailsQuantityPurchased=res.data.records[0].purchaseDetailsList[i].purchaseDetailsQuantityPurchased
                }
            }
        }
    })
    request({
            //查询采购订单
            url:"/inboundDetails/Where",
            method:"post",
            data:inboundDetails.value
        }).then(res=>{
            for(let i = 0;i<res.data.length;i++){
                if(res.data[i]!=null){
                    inboundDetails.value[i].zong=res.data[i].zong-inboundDetails.value[i].inboundDetailsInboundQuantity;
                }else{
                    inboundDetails.value[i].zong=0;
                }
            }
        })

    request({
        //查询仓库
        url:"/warehouse",
        method:"get",
        params:{
            current:1,
            size:10000
    }
    }).then(res=>{
        warehouse.value=res.data.date
    })
}

location();

//是否还需要收货
let isDisabeld=(row)=>{
    if(row==0){
        return true;
    }
    return false;
}
//计算总量
let jisuan=()=>{
    acceptanceOnArrival.value.acceptanceOnArrivalTotalQuantity=0;
    acceptanceOnArrival.value.acceptanceOnArrivalInboundAmount=0;
    for(let i = 0;i<inboundDetails.value.length;i++){
        let isacceptanceOnArrivalEligibility=null;
        if(inboundDetails.value[i].inboundDetailsQuantityOfArrival!=null && inboundDetails.value[i].inboundDetailsUnqualified!=null && inboundDetails.value[i].inboundDetailsQuantityOfArrival>=inboundDetails.value[i].inboundDetailsUnqualified){
            inboundDetails.value[i].inboundDetailsInboundQuantity=inboundDetails.value[i].inboundDetailsQuantityOfArrival-inboundDetails.value[i].inboundDetailsUnqualified
            acceptanceOnArrival.value.acceptanceOnArrivalTotalQuantity+=inboundDetails.value[i].inboundDetailsInboundQuantity;
            acceptanceOnArrival.value.acceptanceOnArrivalInboundAmount+=inboundDetails.value[i].inboundDetailsInboundQuantity*inboundDetails.value[i].supplierPrice.supplierpricesStandardunitprice;
        }
        if(inboundDetails.value[i].inboundDetailsUnqualified!=0&&inboundDetails.value[i].inboundDetailsUnqualified!=null){
            acceptanceOnArrival.value.acceptanceOnArrivalEligibility="是"
            isacceptanceOnArrivalEligibility+=inboundDetails.value[i].inboundDetailsUnqualified;
        }else if(isacceptanceOnArrivalEligibility==0||isacceptanceOnArrivalEligibility==null){
            acceptanceOnArrival.value.acceptanceOnArrivalEligibility="否"
        }
    }
}
</script>
<template>
    <div>
        <el-card>
            <template #header>
                <el-form :model="acceptanceOnArrival" ref="form" :inline="false" class="card_wrap">
                    <el-form-item label="采购单" prop="acceptanceOnArrivalPurchaseOrderId" class="c_item">
                        <el-input v-model="acceptanceOnArrival.acceptanceOnArrivalPurchaseOrderId" disabled class="sousuo" style="width: 150px;"></el-input>
                    </el-form-item>
                    <el-form-item label="入库仓库" prop="acceptanceOnArrivalWarehouseId" class="c_item">
                        <el-select class="sousuo" v-model="acceptanceOnArrival.acceptanceOnArrivalWarehouseId" style="width: 150px;">
                            <el-option v-for="item in warehouse" :label="item.warehouseName" :value="item.warehouseId">
                                </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="入库批次" prop="acceptanceOnArrivalInboundBatches" class="c_item">
                        <el-input v-model="acceptanceOnArrival.acceptanceOnArrivalInboundBatches" class="sousuo" style="width: 150px;"></el-input>
                    </el-form-item>
                    <el-form-item label="入库总数量" prop="acceptanceOnArrivalTotalQuantity" class="c_item">
                        <el-input-number v-model="acceptanceOnArrival.acceptanceOnArrivalTotalQuantity" disabled class="sousuo"></el-input-number>
                    </el-form-item>
                    <el-form-item label="入库总金额" prop="acceptanceOnArrivalInboundAmount" class="c_item">
                        <el-input-number v-model="acceptanceOnArrival.acceptanceOnArrivalInboundAmount" disabled class="sousuo"></el-input-number>
                    </el-form-item>
                    <el-form-item label="是否有不合格产品" prop="acceptanceOnArrivalEligibility" class="c_item">
                        <el-select class="sousuo"v-model="acceptanceOnArrival.acceptanceOnArrivalEligibility" disabled style="width: 150px;">
                            <el-option label="是" value="是">
                                </el-option>
                                <el-option label="否" value="否">
                                </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="质检时间：" prop="acceptanceOnArrivalQualityInspectionTime" class="c_item">
                            <el-date-picker v-model="acceptanceOnArrival.acceptanceOnArrivalQualityInspectionTime" type="date" value-format="YYYY-MM-DD" placeholder="选择日期时间">
                            </el-date-picker>
                    </el-form-item>
                    <el-form-item label="质检员" prop="acceptanceOnArrivalEmployeeId" class="c_item">
                        <el-select class="sousuo"v-model="acceptanceOnArrival.acceptanceOnArrivalEmployeeId" style="width: 150px;">
                            <el-option v-for="item in User" :label="item.nickname" :value="item.id+''">
                                </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="是否入库"  prop="acceptanceOnArrivalStorage" class="c_item">
                        <el-select class="sousuo" :disabled="acceptanceOnArrival.acceptanceOnArrivalStorage === '是'" v-model="acceptanceOnArrival.acceptanceOnArrivalStorage" style="width: 150px;">
                            <el-option label="是" value="是">
                                </el-option>
                                <el-option label="否" value="否">
                                </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="入库时间：" prop="acceptanceOnArrivalInboundTime" class="c_item">
                            <el-date-picker v-model="acceptanceOnArrival.acceptanceOnArrivalInboundTime" type="date" value-format="YYYY-MM-DD" placeholder="选择日期时间">
                            </el-date-picker>
                    </el-form-item>
                    <el-form-item label="入库员" prop="acceptanceOnArrivalWarehousingClerk" class="c_item">
                        <el-select class="sousuo"v-model="acceptanceOnArrival.acceptanceOnArrivalWarehousingClerk" style="width: 150px;">
                            <el-option v-for="item in User" :label="item.nickname" :value="item.id+''">
                                </el-option>
                        </el-select>
                    </el-form-item>

                </el-form>
                
                <el-table :data="inboundDetails" border stripe>
                    <el-table-column prop="inboundDetailsPurchaseDetailsId" label="采购详情编码"/>
                    <el-table-column prop="inboundDetailsArrivalId" label="验收单"/>
                    <el-table-column prop="inboundDetailsQuantityOfArrival" label="到货数量">
                        <template #default="scope">
                            <el-input-number :disabled="isDisabeld(scope.row.purchaseDetailsQuantityPurchased-scope.row.zong) || acceptanceOnArrival.acceptanceOnArrivalStorage === '是'" v-model="scope.row.inboundDetailsQuantityOfArrival" :min="0" :max=scope.row.purchaseDetailsQuantityPurchased-scope.row.zong  :step="1" step-strictly @change="jisuan" @blur="jisuan"></el-input-number>
                        </template>
                    </el-table-column>
                    <el-table-column prop="inboundDetailsUnqualified"  label="不合格数量">
                     <template #default="scope">
                            <el-input-number :disabled="isDisabeld(scope.row.purchaseDetailsQuantityPurchased-scope.row.zong) || acceptanceOnArrival.acceptanceOnArrivalStorage === '是'" v-model="scope.row.inboundDetailsUnqualified" :min="0" :max=scope.row.purchaseDetailsQuantityPurchased-scope.row.zong  :step="1" step-strictly @change="jisuan" @blur="jisuan"></el-input-number>
                        </template>
                    </el-table-column>
                    <el-table-column prop="inboundDetailsInboundQuantity"  label="入库数量">
                     <template #default="scope">
                            <el-input v-model="scope.row.inboundDetailsInboundQuantity" disabled></el-input>
                        </template>
                    </el-table-column>
                    
                </el-table>
                
                <el-form-item>
                    <el-button type="primary" @click="onSubmit">立即修改</el-button>
                </el-form-item>
            </template>
        </el-card>
    </div>
</template>
<style scoped>

.card_wrap {
    width: 98%;
    display: flex;
    flex-wrap: wrap;
    justify-content:flex-start;
}
.c_item{
    flex:1;
    margin:0px 5px 15px 0;
    width: calc((100%-0px)/2); 
    min-width:calc((100% - 10px)/2);
    max-width:calc((100% - 10px)/2);
}
</style>